﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.Statistics.IView;
using MINERP.MODEL.FoundationModel;
using MINERP.Statistics.Presenter;

namespace MINERP.Statistics.Forms
{
    public partial class FrmPerformance : Form, IFrmPerformance
    {
        public FrmPerformance()
        {
            InitializeComponent();
        }

        private static FrmPerformance instance = null;
        public static FrmPerformance Instance
        {
            get
            {

                if (instance == null)
                {
                    instance = new FrmPerformance();
                }
                if (instance.IsDisposed)
                {
                    instance = new FrmPerformance();
                }
                return instance;
            }
        }
        //#region IFrmPerformance 成员

        //int GroupId
        //{
        //    get;

        //    set;

        //}

        //DateTime IFrmPerformance.StartTime
        //{
        //    get;

        //    set;

        //}

        //DateTime IFrmPerformance.EndTime
        //{
        //    get;

        //    set;

        //}

        //void BandingGroup(List<Tb_WorkerGroup_Info> list)
        //{
        //    cmbGroup.DisplayMember = "GroupName";
        //    cmbGroup.ValueMember = "GroupId";
        //    cmbGroup.DataSource = list;
        //}

        //#endregion

        private void simpleButtonOk_Click(object sender, EventArgs e)
        {
            
            this.Cursor = Cursors.WaitCursor;
            this.simpleButtonOk.Enabled = false;
            report.Preview = previewControl1;
            report.Load(Application.StartupPath + "\\Performance.frx");
            report.RegisterData(_perserter.GetPerformance());

            report.SetParameterValue("startTime", dateEditStartTime.DateTime.Date);
            report.SetParameterValue("endTime", dateEditEndTime.DateTime.Date);
            previewControl1.Refresh();
            previewControl1.Show();



            try
            {
                report.Show();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            finally
            {
                this.simpleButtonOk.Enabled = true;
            }
            this.Cursor = Cursors.Default;

        }
        PPerformance _perserter;
        private void FrmPerformance_Load(object sender, EventArgs e)
        {
            dateEditStartTime.DateTime = DateTime.Now.Date.Subtract(new TimeSpan(1, 0, 0, 0));
            dateEditEndTime.DateTime = DateTime.Now.Date;

            _perserter = new PPerformance(this);
            _perserter.Init();
        }

        #region IFrmPerformance 成员


        void IFrmPerformance.BandingGroup(List<Tb_WorkerGroup_Info> list)
        {
            cmbGroup.DisplayMember = "GroupName";
            cmbGroup.ValueMember = "GroupId";
            cmbGroup.DataSource = list;
        }

        #endregion

        private void cmbGroup_SelectionChangeCommitted(object sender, EventArgs e)
        {
            GroupId = ((Tb_WorkerGroup_Info)this.cmbGroup.SelectedItem).GroupId;
            _perserter.BindWorkerList(GroupId);
        }

        #region IFrmPerformance 成员

        public int GroupId
        {
            get;
            set;
        }

        DateTime _startTime;
        public DateTime StartTime
        {
            get { return dateEditStartTime.DateTime.Date; }
            //set { _startTime = value; }
        }

        public DateTime EndTime
        {
            get { return dateEditEndTime.DateTime.Date.AddDays(1); }
            //set;
        }

        #endregion

        private void FrmPerformance_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

        #region IFrmPerformance 成员


        public void BindUser(List<string> userList)
        {
            this.cmbWorker.Properties.Items.Clear();
            //this.cmbWorker.DisplayMember = "UserName";
            //cmbWorker.ValueMember = "UserName";
            //cmbWorker.DataSource = userList;
            this.cmbWorker.Properties.Items.Add("全部");

            foreach (string u in userList)
            {
                this.cmbWorker.Properties.Items.Add(u);
            }
            cmbWorker.Text = "全部";

        }

        #endregion

        #region IFrmPerformance 成员


        public string GroupName
        {
            get
            {
                return cmbGroup.Text.Trim();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string UserName
        {
            get
            {
                return cmbWorker.Text.Trim();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
